package dang.interceptor;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

import dang.common.HibernateUtil;

public class TransactionInterceptor extends MethodFilterInterceptor {

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		Session s=HibernateUtil.getSession();
		System.out.println(s.hashCode());
		Transaction t =s.beginTransaction();
		try{
			String resultCode = invocation.invoke();//执行Action业务方法
			t.commit();
			return resultCode;
		}catch(Exception e){
			//TODO 记录异常信息,写入日志文件
			e.printStackTrace();
			t.rollback();
			return "error";
		}
	}

}
